package com.freak.wanandroid.model.discover;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.freak.wanandroid.R;
import com.freak.wanandroid.base.adapter.MainTabAdapter;
import com.freak.wanandroid.base.fragment.BaseLifecycleFragment;
import com.freak.wanandroid.model.discover.tab.CourseFragment;
import com.freak.wanandroid.model.discover.tab.FaqFragment;
import com.freak.wanandroid.model.discover.tab.LatestProjectFragment;
import com.freak.wanandroid.model.discover.tab.LearningPathFragment;
import com.freak.wanandroid.model.discover.tab.SquareFragment;
import com.freak.wanandroid.model.discover.tab.ToolFragment;
import com.freak.wanandroid.model.navigation.NavigationFragment;
import com.freak.wanandroid.model.system.SystemFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 发现tab页
 *
 * @author: freak
 * @date: 2022/9/25
 */
public class DiscoverFragment extends BaseLifecycleFragment<DiscoverViewModel> {
    private List<String> mList;

    private List<Fragment> mFragmentList;

    private MainTabAdapter mMainTabAdapter;

    private ViewPager2 mViewPager2;

    private TabLayout mTableLayout;

    private final static Map<String, Fragment> STRING_FRAGMENT_HASH_MAP = new LinkedHashMap<>();

    static {
        STRING_FRAGMENT_HASH_MAP.put("最新项目", new LatestProjectFragment());
        STRING_FRAGMENT_HASH_MAP.put("广场", new SquareFragment());
        STRING_FRAGMENT_HASH_MAP.put("体系", new SystemFragment());
        STRING_FRAGMENT_HASH_MAP.put("导航", new NavigationFragment());
        STRING_FRAGMENT_HASH_MAP.put("教程", new CourseFragment());
        STRING_FRAGMENT_HASH_MAP.put("问答", new FaqFragment());
        STRING_FRAGMENT_HASH_MAP.put("工具", new ToolFragment());
        STRING_FRAGMENT_HASH_MAP.put("学习路线", new LearningPathFragment());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_discover;
    }

    @Override
    protected void initEventAndData(View view) {
        mList = new ArrayList<>();
        mFragmentList = new ArrayList<>();
        for (Map.Entry<String, Fragment> stringFragmentEntry : STRING_FRAGMENT_HASH_MAP.entrySet()) {
            mList.add(stringFragmentEntry.getKey());
            mFragmentList.add(stringFragmentEntry.getValue());
        }
        mMainTabAdapter = new MainTabAdapter(mCurrentActivity, mFragmentList, mList);
        mViewPager2 = getViewById(R.id.viewPageDisCover);
        mTableLayout = getViewById(R.id.tableLayoutDisCover);
        mViewPager2.setAdapter(mMainTabAdapter);
        mViewPager2.setOffscreenPageLimit(1);
        new TabLayoutMediator(mTableLayout, mViewPager2, (tab, position) -> tab.setText(mList.get(position))).attach();
    }

    @Override
    protected void onResumeLoadData() {

    }

    @Override
    protected void dataObserver() {

    }
}
